/*
 * 5.1
 * You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a
 * method to set all bits between i and j in N equal to M (e.g., M becomes a substring of
 * N located at i and starting at j).
 * EXAMPLE:
 * Input: N = 10000000000, M = 10101, i = 2, j = 6
 * Output: N = 10001010100
 *
 */
#include <stdio.h>

int setbits(int n, int m, int i, int j)
{
    int hi = (n >> j) << j;
    int lo = n & ((1<<i) - 1);
    return (m << i) | hi | lo;
}

int main()
{
    int r = setbits(1024, 21, 2, 6);
    printf("r=%d\n", r);
    return 0;
}
